package repo

import (
	"gitee.com/linxing_3/sr-core/domain/repo"
	"gitee.com/linxing_3/sr-core/plugins/storage/cache"
	"gitee.com/linxing_3/sr-core/plugins/storage/database"
	"gitee.com/mkwchecking/sr-admin/apps/devtools/entity"
)

func NewModuleField(dataset database.IDataSet, cache cache.ICache) *ModuleField {
	return &ModuleField{
		IRepo: repo.NewSet[entity.ModuleField](dataset, cache),
	}
}

type ModuleField struct {
	repo.IRepo[entity.ModuleField]
}

func (s *ModuleField) Replace(domain, table string, fields ...*entity.ModuleField) error {

	cond := &entity.ModuleField{
		Domain:    domain,
		ClassName: table,
	}

	err := s.ForceDeleteWithEnt(cond)

	if err != nil {
		return err
	}

	for _, field := range fields {
		field.Domain = domain
		field.ClassName = table
	}

	_, err = s.CreateMulti(fields)
	return err
}
